from werkzeug.routing import BaseConverter
from flask import session, jsonify, current_app
from app.utils.code import RET
import functools


class ReConverter(BaseConverter):
    def __init__(self, url_map, regex):
        # print(url_map)
        # 调用父类的初始化方法
        super(ReConverter, self).__init__(url_map)
        # 保存正则表达式
        self.regex = regex


""" 定义的验证登录状态的装饰器 """
def login_required(fun):
    @functools.wraps(fun)
    def is_login(*args, **kw):
        if "user_id" in session:
            user_id = session['user_id']
            if user_id is not None:
                return fun(*args, **kw)
        return jsonify(errno = RET.LOGINERR, errmsg = "用户未登录")   
    return is_login